const { getOrders } = require('../dao/adminOrdersDao');

module.exports.getOrders = async function(query) {
    const { input, value } = query;
    const orders = await getOrders(query);
    if (input == undefined || value == undefined) {
        return orders;
    } else if (input && value) {
        if (value == 'name') {
            const data = orders.data.filter(item => {
                if (item.tenantsId.name.indexOf(input) != -1) {
                    return item;
                }
            })
            return {
                data,
                total: data.length
            };
        } else if (value == 'account') {
            const data = orders.data.filter(item => {
                if (item.parkingsId.lessorsId.account.indexOf(input) != -1) {
                    return item;
                }
            })
            return {
                data,
                total: data.length
            };
        } else if (value == 'number') {
            const data = orders.data.filter(item => {
                if (item.parkingsId.number.indexOf(input) != -1) {
                    return item;
                }
            })
            return {
                data,
                total: data.length
            };
        }

    }
}